package com.kedao.util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期格式转换
 * 
 * @author Administrator
 * 
 */
public class DateUtils {
	

	// 当前月的第一天
	public static String getFristDayByMonth() {
		Calendar cal = Calendar.getInstance();
		SimpleDateFormat datef = new SimpleDateFormat("yyyy-MM-dd");
		cal.set(Calendar.DAY_OF_MONTH, 1);
		Date beginTime = cal.getTime();
		return datef.format(beginTime) + " 00:00:00";
	}

	// 当前月的最后一天
	public static String getLastDayByMonth() {
		Calendar cal = Calendar.getInstance();
		SimpleDateFormat datef = new SimpleDateFormat("yyyy-MM-dd");
		cal.set(Calendar.DATE, 1);
		cal.roll(Calendar.DATE, -1);
		Date endTime = cal.getTime();
		return datef.format(endTime) + " 23:59:59";
	}

	public static Date getDate() {
		Date dateU = new Date();
		java.sql.Date date = new java.sql.Date(dateU.getTime());
		return date;
	}

	public static Date getDate(String date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date d = null;
		try {
			d = sdf.parse(date);
		} catch (java.text.ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return d;
	}

	public static String getDate(String date, String format) {
		if (format == null)
			format = "yyyy-MM-dd HH:mm:ss";
		String result = "";
		DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
		try {
			Date d = df.parse(date);
			SimpleDateFormat sdf = new SimpleDateFormat(format);
			result = sdf.format(d);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return result;
	}

	public static String getCutStr(String date) {
		return null;
	}

	public static String getDateTime() {
		SimpleDateFormat sdf;
		Date date = null;
		Calendar myDate = Calendar.getInstance();
		myDate.setTime(new Date());
		date = myDate.getTime();
		sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sdf.format(date);
	}

	public static String getDateNoStyle() {
		SimpleDateFormat sdf;
		Date date = null;
		Calendar myDate = Calendar.getInstance();
		myDate.setTime(new Date());
		date = myDate.getTime();
		sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		return sdf.format(date);
	}

	public static String getDateNoStyle_2() {
		SimpleDateFormat sdf;
		Date date = null;
		Calendar myDate = Calendar.getInstance();
		myDate.setTime(new Date());
		date = myDate.getTime();
		sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.format(date);
	}
	
	public static String getDateNoStyle_3() {
		SimpleDateFormat sdf;
		Date date = null;
		Calendar myDate = Calendar.getInstance();
		myDate.setTime(new Date());
		date = myDate.getTime();
		sdf = new SimpleDateFormat("yyyyMMdd");
		return sdf.format(date);
	}

	public static String formatDate(Date date, String format) {
		if (format == null)
			format = "yyyy-MM-dd HH:mm:ss";
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(date);
	}

	// 比较2个日期相差多少小时
	public static String compareDateDiffer(String date) {
		// yyyyMMddHHmmss
		String result = "";
		DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
		try {
			Date d = df.parse(date);
			Date now = new Date();
			long diff = now.getTime() - d.getTime();
			// 天
			long day = diff / (24 * 60 * 60 * 1000);
			// 小时
			long hour = diff / (60 * 60 * 1000);
			// 分钟
			long min = diff / (60 * 1000);
			// 秒
			long sec = (diff / 1000);

			if (sec > 0 && sec <= 5) {
				return "刚刚";
			}
			if (sec > 0 && sec <= 60) {
				return sec + "秒前";
			}
			if (min > 0 && min <= 60) {
				return min + "分钟前";
			}
			if (hour > 0 && hour <= 24) {
				return hour + "小时前";
			}
			if (day > 0) {
				return day + "天前";
			}

		} catch (Exception e) {
			// TODO: handle exception
			return "d-error";
		}
		return result;
	}

	// 得到前*天(yyyyMMdd)
	public static String getBeforeDayNoStyle(Integer day) {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DATE, -day); // 得到前*天
		Date date = calendar.getTime();
		DateFormat df = new SimpleDateFormat("yyyyMMdd");
		return df.format(date);
	}

	// 向后推迟几天
	public static String getNextDay(Integer day) {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DAY_OF_MONTH, day);
		Date date = calendar.getTime();
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return df.format(date);
	}

	// 得到前*分钟(yyyyMMddHHmmss)
	public static String getBeforeMinNoStyle_yyyyMMddHHmmss(Integer min) {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.MINUTE, -min); // 得到前*分钟
		Date date = calendar.getTime();
		DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
		return df.format(date);
	}

	// 得到前*天(yyyyMMddHHmmss)
	public static String getBeforeDayNoStyle_yyyyMMddHHmmss(Integer day) {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DATE, -day); // 得到前*天
		Date date = calendar.getTime();
		DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
		return df.format(date);
	}

	// 得到前*天(yyyy-MM-dd)
	public static String getBeforeDayStyle_line(Integer day) {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DATE, -day); // 得到前*天
		Date date = calendar.getTime();
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		return df.format(date);
	}

	// 得到前*天(yyyy-MM-dd)
	public static String getBeforeDayStyle_line(String currentDate, Integer day) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(getDate(currentDate));
		calendar.add(Calendar.DATE, -day); // 得到前*天
		Date date = calendar.getTime();
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		return df.format(date);
	}

	public static String getBeforeDayStyle_point(Integer day) {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DATE, -day); // 得到前*天
		Date date = calendar.getTime();
		DateFormat df = new SimpleDateFormat("yyyyMMdd");
		return df.format(date).substring(4, 6) + "."
				+ df.format(date).substring(6, 8);
	}

	public static String getBeforeDayStyle_point(String currentDate, Integer day) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(getDate(currentDate));
		calendar.add(Calendar.DATE, -day); // 得到前*天
		Date date = calendar.getTime();
		DateFormat df = new SimpleDateFormat("yyyyMMdd");
		return df.format(date).substring(4, 6) + "."
				+ df.format(date).substring(6, 8);
	}

	// // 获取日期范围的分组
	// public static String[] getDateGroup_yyyyMMdd(String startDate, String
	// endDate) {
	// String[] result = null;
	// SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	// Date start = null;
	// Date end = null;
	// try {
	// start = sdf.parse(startDate);
	// end = sdf.parse(endDate);
	// long diff = end.getTime() - start.getTime();
	// // 天
	// long day = diff / (24 * 60 * 60 * 1000); // 这个方法有问题
	// int group = Integer.parseInt(Long.toString(day))+1;
	// result = new String[group];
	// for (int i = 0; i < result.length; i++) {
	// result[i] = getBeforeDayStyle_line(endDate, i);
	// }
	// } catch (Exception e) {
	// e.printStackTrace();
	// }
	// return result;
	// }

	public static String[] getDateGroup(String startDate, String endDate) {
		String[] result = null;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date start = null;
		Date end = null;
		try {
			start = sdf.parse(startDate);
			end = sdf.parse(endDate);
			long diff = end.getTime() - start.getTime();
			// 天
			long day = diff / (24 * 60 * 60 * 1000); // 这个方法有问题
			int group = Integer.parseInt(Long.toString(day)) + 1;
			result = new String[group];
			for (int i = 0; i < result.length; i++) {
				result[i] = getBeforeDayStyle_point(endDate, i);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	public static String[] getDateGroup_yyyyMMdd(String startDate,
			String endDate) {
		String[] result = null;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date start = null;
		Date end = null;
		try {
			start = sdf.parse(startDate);
			end = sdf.parse(endDate);
			long diff = end.getTime() - start.getTime();
			// 天
			long day = diff / (24 * 60 * 60 * 1000); // 这个方法有问题
			int group = Integer.parseInt(Long.toString(day)) + 1;
			result = new String[group];
			for (int i = 0; i < result.length; i++) {
				result[i] = getBeforeDayStyle_line(endDate, i);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	// 获取24小时的分组
	public static String[] getHourGroup() {
		int group = 4;
		String[] array = new String[24];
		for (int i = 0; i < array.length; i++) {
			if (i == 0 || i % (array.length / group) == 0) {
				String temp = i + "";
				if (temp.length() < 2) {
					temp = "0" + temp;
				}
				array[i] = temp + ":00-" + temp + ":59";
			} else {
				array[i] = "";
			}
		}
		return array;
	}

	// 每周第几天
	public static boolean isWeekMonthsDay(int currentWeekDay) {
		boolean b = false;
		Calendar c = Calendar.getInstance();
		int dayForWeek = 0;
		if (c.get(Calendar.DAY_OF_WEEK) == 1) {
			dayForWeek = 7;
		} else {
			dayForWeek = c.get(Calendar.DAY_OF_WEEK) - 1;
		}
		System.out.println("温馨提示：今天是这周的第" + dayForWeek + "天");
		if (dayForWeek == currentWeekDay) {
			b = true;
		}
		return b;
	}

	// 每月第几天
	public static boolean isEveryMonthsDay(int currentDay) {
		boolean b = false;
		Calendar c = Calendar.getInstance();
		int day = c.get(Calendar.DAY_OF_MONTH);
		System.out.println("温馨提示：今天是这月的第" + day + "天");
		if (day == currentDay) {
			b = true;
		}
		return b;
	}

	public static void main(String[] args) {
		//
		// UUID uuid = UUID.randomUUID();
		// String str = uuid.toString();
		// System.out.println(str);
		//
		// System.out.println(DateUtils.getDate("20130713012817", null));
		//
		// System.out.println(DateUtils.getDateNoStyle());
		// System.out.println(DateUtils.getBeforeDayNoStyle_yyyyMMddHHmmss(1));
		//
		// System.out.println(getHourGroup());
		// System.out.println(getDateGroup("2013-06-16", "2013-07-16"));
		// for (int i = 0; i < getHourGroup().length; i++) {
		// System.out.println("==" + getHourGroup()[i]);
		// }

		// System.out.println(DateUtils.getBeforeMinNoStyle_yyyyMMddHHmmss(3));
		// System.out.println(DateUtils.getNextDay(3));
//		System.out.println(DateUtils.getFristDayByMonth());
		System.out.println(DateUtils.getBeforeDayStyle_line(0));
	}

}
